//
//  SearchResultCell.m
//  AmazingShopping
//
//  Created by Tan on 4/30/13.
//  Copyright (c) 2013 Tan Hoang. All rights reserved.
//

#import "SearchResultCell.h"

@implementation SearchResultCell

@synthesize productName, sellerName, price, productImagePath, productRate, reviewNumber;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setProductName:(NSString *)pn{
    if (![productName isEqualToString:pn]) {
        productName = [pn copy];
        productNameLabel.text = productName;
    }
}

- (void)setSellerName:(NSString *)sn{
    if (![sellerName isEqualToString:sn]) {
        sellerName = [sn copy];
        sellerLabel.text = sellerName;
    }
}

- (void)setPrice:(NSString *)pr{
    if (![price isEqualToString:pr]) {
        price = [pr copy];
        priceLabel.text = [NSString stringWithFormat:@"$%@", price];
    }
}

- (void)setProductImagePath:(NSString *)imagePath{
    if (![productImagePath isEqualToString:imagePath]) {
        productImagePath = [imagePath copy];
        NSData *imageData = [[NSData alloc]initWithContentsOfURL: [NSURL URLWithString:productImagePath]];
        [productImageView setImage:[UIImage imageWithData:imageData]];
    }
}

- (void)setProductRate:(float)pr{
    if (pr <= 5) {
        productRate = pr;
    }
    
    int temp = (int)productRate;
    
    if (productRate == 0) {
        for (int i = 0; i < [rateImageView count]; i++) {
            [[rateImageView objectAtIndex:i] setImage:[UIImage imageNamed:@"rate0.png"]];
        }
    }else{
        for (int i = 0; i < temp; i++) {
            [[rateImageView objectAtIndex:i] setImage:[UIImage imageNamed:@"rate1.png"]];
        }
        if (productRate - temp > 0) {
            [[rateImageView objectAtIndex:temp] setImage:[UIImage imageNamed:@"rate2.png"]];
        }else{
            [[rateImageView objectAtIndex:4] setImage:[UIImage imageNamed:@"rate0.png"]];
        }
        
        for (int i = temp + 1; i < [rateImageView count]; i++) {
            [[rateImageView objectAtIndex:i] setImage:[UIImage imageNamed:@"rate0.png"]];
        }
    }
}

- (void)setReviewNumber:(NSInteger*)rn{
    reviewNumber = rn;
    rateLabel.text = [NSString stringWithFormat:@"(%ld)", (long) reviewNumber];
}

@end
